package com.yuewen.mall.common.mallcacher;

import com.yuewenmall.common.baseentities.common.PageProductSetInfo;
import com.yuewenmall.common.baseentities.common.RequestInfo;
import com.yuewenmall.common.baseentities.product.ResponseData.FullProductSetInfo;
import com.yuewenmall.common.baseentities.product.SearchRequestDataInfo;
import com.yuewenmall.common.baseentities.product.SearchResultInfo;
import com.yuewenmall.common.baseentities.product.dbentityimpl.ProductAttributeInfo;
import com.yuewenmall.common.baseentities.product.dbentityimpl.ProductCapacityInfo;
import com.yuewenmall.common.utilities.ValidatorUtility;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liwei.b on 2016/6/30.
 */
public class ProductCacheManager extends CommonCacherManager {
    public static ProductCacheManager Instance = null;

    static {
        Instance = new ProductCacheManager();
    }

    private ProductCacheManager() {
    }

    public List<ProductCapacityInfo> getAllProductCapacityInfos() {
        String key = ProductCacheKeyManager.getAllProductCompacityCacheKey();
        List<ProductCapacityInfo> tmpResult = redisClient.get(new ArrayList<ProductCapacityInfo>() {
        }.getClass(), key);
        return tmpResult;
    }

    public String updateAllProductCapacityInfos(List<ProductCapacityInfo> value) {
        String key = ProductCacheKeyManager.getAllProductCompacityCacheKey();
        return redisClient.setObject(key, value, ProductCacheKeyManager.getAllProductCompacityCacheTimeout());
    }

    /**
     * 获取搜索结果的缓存
     *
     * @param req 搜索条件
     * @return
     */
    public SearchResultInfo getDoSearchCache(RequestInfo<SearchRequestDataInfo> req) {
        if (req == null || req.getData() == null) {
            return null;
        }
        String key = ProductCacheKeyManager.getDoSearchCacheKey(req.getData());
        SearchResultInfo tmpResult = redisClient.get(SearchResultInfo.class, key);
        return tmpResult;
    }

    /**
     * 更新搜索页面 搜索结果 缓存
     *
     * @param req   搜索条件
     * @param value 搜索结果
     * @return
     */
    public String updateDoSearchCache(RequestInfo<SearchRequestDataInfo> req, SearchResultInfo value) {
        if (req == null || req.getData() == null) {
            return "";
        }
        String key = ProductCacheKeyManager.getDoSearchCacheKey(req.getData());
        return redisClient.setObject(key, value, ProductCacheKeyManager.getDoSearchCacheKeyTimeout());
    }

    /**
     * 根据商品集id， 缓存这个商品集全部商品的属性信息
     *
     * @param productSetId 商品集id
     * @return
     */
    public List<ProductAttributeInfo> getAttributesByProductSetId(BigInteger productSetId) {
        if (ValidatorUtility.isLessThanOrEqual(productSetId, 0)) {
            return null;
        }
        String key = ProductCacheKeyManager.getProductAttributesByProductSetIdCacheKey(productSetId);
        List<ProductAttributeInfo> tmpResult = redisClient.get(new ArrayList<ProductAttributeInfo>() {
        }.getClass(), key);
        return tmpResult;
    }

    /**
     * 根据商品集id， 更新这个商品集全部商品的属性缓存信息
     *
     * @param productSetId 商品集id
     * @param value        新的全部属性信息
     * @return
     */
    public String updateGetAttributesByProductSetId(BigInteger productSetId, List<ProductAttributeInfo> value) {
        if (ValidatorUtility.isLessThanOrEqual(productSetId, 0)) {
            return "";
        }
        String key = ProductCacheKeyManager.getProductAttributesByProductSetIdCacheKey(productSetId);
        return redisClient.setObject(key, value, ProductCacheKeyManager.getProductAttributesByProductSetIdCacheKeyTimeout());
    }

    /**
     * 获取全部商品信息
     *
     * @return
     */
    public List<PageProductSetInfo> getAllProducts() {
        String key = ProductCacheKeyManager.getAllProductCacheKey();
        return redisClient.get(new ArrayList<PageProductSetInfo>() {
        }.getClass(), key);
    }

    /**
     * 更新全部商品商品信息
     *
     * @param value
     * @return
     */
    public String updateGetAllProducts(List<PageProductSetInfo> value) {
        String key = ProductCacheKeyManager.getAllProductCacheKey();
        return redisClient.setObject(key, value, ProductCacheKeyManager.getAllProductCacheKeyTimeout());
    }

    /**
     * 获取完整的商品信息 = 一个商品集信息 + 这个商品集全部商品信息
     *
     * @param productSetId
     * @return
     */
    public FullProductSetInfo getFullProductByProductSetId(BigInteger productSetId) {
        String key = ProductCacheKeyManager.getFullProductCacheKey(productSetId);
        return redisClient.get(FullProductSetInfo.class, key);
    }

    /**
     * 更新一个完整商品信息 = 一个商品集信息 + 这个商品集全部商品信息
     * @param productSetId
     * @param value
     * @return
     */
    public String updateFullProductByProductSetId(BigInteger productSetId,FullProductSetInfo value ) {
        String key = ProductCacheKeyManager.getFullProductCacheKey(productSetId);
        return redisClient.setObject(key, value, ProductCacheKeyManager.getFullProductCacheKeyTimeout());
    }
}
